﻿cmake_minimum_required (VERSION 3.8)

project ("MyOpenGL")
set(PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR})

set(C_STANDARD 99)
set(CXX_STANDARD 14)

if (CXX_STANDARD EQUAL 17)
  if (MSVC_VERSION GREATER_EQUAL "1900")
	include(CheckCXXCompilerFlag)
	CHECK_CXX_COMPILER_FLAG("/std:c++17" _cpp_17_flag_supported)
	if (_cpp_17_flag_supported)
	  set(CMAKE_CXX_STANDARD 17)
	endif()
  endif()
endif()

message(STATUS CMAKE_CXX_COMPILER_ID= "${CMAKE_CXX_COMPILER_ID}")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  if (CMAKE_BUILD_TYPE MATCHES "Debug")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEBUG")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_DEBUG")
  elseif (CMAKE_BUILD_TYPE MATCHES "Release")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -O2")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O2")
  endif()
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU|Clang")
  if (CMAKE_BUILD_TYPE MATCHES "Debug")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -D_DEBUG")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -D_DEBUG")
  elseif (CMAKE_BUILD_TYPE MATCHES "Release")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -O3 -Wall -Wextra")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -Wall -Wextra")
  endif()
endif()

#set(ENV{PATH} "F:/VisualStudioProjects/3rdparty/opencv-4.7.0/build/x64/vc16/bin:$ENV{PATH}")
set(OpenCV_DIR "F:/VisualStudioProjects/3rdparty/opencv-4.7.0/build")
### Windows Debug模式下会去找debug库
#set(OpenCV_DIR "F:/VisualStudioProjects/3rdparty/opencv-4.7.0-win64_with_ffmpeg/${CMAKE_BUILD_TYPE}")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})

add_library(
	mygl 
	# glad
	"src/glad.c"
	# mygl
	"src/mygl/shader.cpp"
	"src/mygl/compute_shader.cpp"
	"src/mygl/util.cpp"
	"src/mygl/es_image.cpp" 
	"src/mygl/camera.cpp" 
	"src/mygl/texture2d.cpp" 
	"src/mygl/resource_manager.cpp" 
	"src/mygl/sprite_renderer.cpp" 
	"src/mygl/text_renderer.cpp"
	"src/mygl/yuv_texture2d.cpp"
	"src/mygl/static_renderer.cpp"
	"src/mygl/video_renderer.cpp"
	"src/mygl/video_postprocessing.cpp"
	# game
	"src/mygl/game/game.cpp" 
	"src/mygl/game/game_object.cpp" 
	"src/mygl/game/game_level.cpp" 
	"src/mygl/game/ball_object.cpp" 
	"src/mygl/game/particle_generator.cpp" 
	"src/mygl/game/post_processing.cpp" 
	# model
	"src/mygl/model/mesh.cpp"
	"src/mygl/model/model.cpp"
) 
target_include_directories(mygl PUBLIC "include" "include/freetype2")
target_link_directories(mygl PUBLIC lib)
target_link_libraries(mygl PUBLIC ${OpenCV_LIBS} glfw3 freetype)
target_compile_definitions(mygl PUBLIC "-DPROJECT_ROOT=\"${PROJECT_ROOT}\"")

if (CMAKE_BUILD_TYPE MATCHES "Debug")
  target_link_libraries(mygl PUBLIC assimp-vc143-mtd zlibstaticd)
  #target_link_libraries(mygl PUBLIC assimp)
elseif(CMAKE_BUILD_TYPE MATCHES "Release")
  target_link_libraries(mygl PUBLIC assimp-vc143-mt zlibstatic)
endif()

set(INSTALL_DEP ${PROJECT_ROOT}/lib/glfw3.lib
				${PROJECT_ROOT}/lib/freetype.lib
)
set(INSTALL_CV ${OpenCV_DIR}/x64/vc16)
install(TARGETS mygl)
install(FILES ${INSTALL_DEP} DESTINATION lib)
install(DIRECTORY ${INSTALL_CV}/lib/ DESTINATION lib) 
install(DIRECTORY ${INSTALL_CV}/bin/ DESTINATION bin) 

# 包含子项目。
#add_subdirectory ("01-GettingStarted")
#add_subdirectory ("02-Lighting")
#add_subdirectory ("03-ModelLoading")
add_subdirectory ("04-AdvancedOpenGL")
#add_subdirectory ("05-AdvancedLighting")
#add_subdirectory ("06-PBR")
#add_subdirectory ("07-InPractice")

#add_subdirectory ("MyTest")
#add_subdirectory ("LenovoTest")